<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Domain extends MY_Controller {

	function index() {
		header("Location: /domain/manage");
	}

	function service() {
		$this->load->model('service');
		$this->data['service'] = $this->service->all();
		$this->load->view('domain/service', array('data' => $this->data));
	}	

	function service_add() {
		$data['service'] = trim($this->input->post('service', TRUE));

		$this->load->model('service');
		$r = $this->service->filter($data);
		if ($r['status']){
			$this->service->add($data);
			echo json_encode(array('r'=>'succ', 'msg' => 'ok'));
		} else {
			echo json_encode(array('r'=>'fail', 'msg' => $r['msg']));
		}
	}

	function service_del() {
		$id = $this->input->post('service_id', TRUE);
		$this->load->model('service');
		if (intval($id) == 0) {
			echo json_encode(array('r' => 'fail', 'msg' => '业务ID为空'));
			return;
		}
		$this->service->del($id);
		echo json_encode(array('r' => 'succ', 'msg' => 'ok'));
	}

	function manage() {
		$this->load->model('cdn_domain');
		$this->data['unbind_domain'] = $this->cdn_domain->get_unbind_domain();
		$this->load->view('domain/manage', array('data' => $this->data));
	}

	function manage_add() {
		$data['stat_mode'] = $this->input->post('stat_mode', TRUE);
		$data['charge_mode'] = $this->input->post('charge_mode', TRUE);
		$data['fee_mode'] = $this->input->post('fee_mode', TRUE);
		$data['cache_mode'] = $this->input->post('cache_mode', TRUE);
		$data['log_mode'] = $this->input->post('log_mode', TRUE);
		$data['analysis_mode'] = $this->input->post('analysis_mode', TRUE);
		$data['domain'] = trim($this->input->post('domain', TRUE));

		$this->load->model('cdn_domain');
		$r = $this->cdn_domain->filter($data);
		if ($r['status']){
			$this->cdn_domain->add($data);
			echo json_encode(array('r'=>'succ', 'msg' => 'ok'));
		} else {
			echo json_encode(array('r'=>'fail', 'msg' => $r['msg']));
		}
	}

	function manage_del() {
		$id = $this->input->post('domain_id', TRUE);
		$this->load->model('cdn_domain');
		if (intval($id) == 0) {
			echo json_encode(array('r' => 'fail', 'msg' => '域名ID为空'));
			return;
		}
		$this->cdn_domain->del($id);
		echo json_encode(array('r' => 'succ', 'msg' => 'ok'));
	}

	function unit() {
		$this->load->model('service');
		$this->load->model('cdn_user');
		$this->data['all_user'] = $this->cdn_user->all_user(0, 100000);
		$this->data['service'] = $this->service->all();

		$this->load->view('domain/unit', array('data' => $this->data));
	}

	function unit_search() {
		$this->load->model('unit');
		$this->load->model('cdn_user');
		$this->load->model('service');
		$cond['service_id'] = intval($this->input->get('service_id', TRUE));
		$cond['user_id'] = intval($this->input->get('user_id', TRUE));
		$this->data['total'] = $this->unit->total($cond);
		$cond['page'] = intval($this->input->get('page', TRUE));
		$cond['per_page'] = 10;
		$unit = $this->unit->cfind($cond);
		foreach ($unit as $k => $v) {
			$u = $this->cdn_user->find($v['user_id']);
			$unit[$k]['username'] = $u ? $u[0]['username'] : '';
			$s = $this->service->find($v['service_id']);
			$unit[$k]['service'] = $s ? $s[0]['service'] : '';
			$charge_mode = array('1' => '流量', '2' => '下载量');
			$unit[$k]['charge_mode'] = $charge_mode[$v['charge_mode']];
			$fee_mode = array('1' => 'A', '2' => 'B', '3' => 'C');
			$unit[$k]['fee_mode'] = $fee_mode[$v['fee_mode']];
		}

		$this->data['unit'] = $unit;
		$this->load->view('domain/unit_search', array('data' => $this->data));
	}

	function unit_add() {
		$data['unit'] = trim($this->input->post('unit', TRUE));
		$data['user_id'] = $this->input->post('user_id', TRUE);
		$data['service_id'] = $this->input->post('service_id', TRUE);
		$data['charge_mode'] = $this->input->post('charge_mode', TRUE);
		$data['fee_mode'] = $this->input->post('fee_mode', TRUE);
		$data['unit'] = $this->input->post('unit', TRUE);

		$this->load->model('unit');
		$r = $this->unit->filter($data);
		if ($r['status']){
			$this->unit->add($data);
			echo json_encode(array('r'=>'succ', 'msg' => 'ok'));
		} else {
			echo json_encode(array('r'=>'fail', 'msg' => $r['msg']));
		}
	}

	function unit_del() {
		$id = $this->input->post('unit_id', TRUE);
		$this->load->model('unit');
		if (intval($id) == 0) {
			echo json_encode(array('r' => 'fail', 'msg' => '域名ID为空'));
			return;
		}
		$this->unit->del($id);
		echo json_encode(array('r' => 'succ', 'msg' => 'ok'));
	}

	function bind() {
		$this->load->model('service');
		$this->load->model('cdn_user');
		$this->data['all_user'] = $this->cdn_user->all_user(0, 100000);
		$this->data['service'] = $this->service->all();
		$this->load->view('domain/bind', array('data' => $this->data));
	}
	function bind_search() {
		$this->load->model('unit');
		$this->load->model('cdn_user');
		$this->load->model('service');
		$cond['service_id'] = intval($this->input->get('service_id', TRUE));
		$cond['user_id'] = intval($this->input->get('user_id', TRUE));
		$this->data['total'] = $this->unit->total($cond);
		$cond['page'] = intval($this->input->get('page', TRUE));
		$cond['per_page'] = 10;
		$unit = $this->unit->cfind($cond);
		foreach ($unit as $k => $v) {
			$u = $this->cdn_user->find($v['user_id']);
			$unit[$k]['username'] = $u ? $u[0]['username'] : '';
			$s = $this->service->find($v['service_id']);
			$unit[$k]['service'] = $s ? $s[0]['service'] : '';
			$charge_mode = array('1' => '流量', '2' => '下载量');
			$unit[$k]['charge_mode'] = $charge_mode[$v['charge_mode']];
			$fee_mode = array('1' => 'A', '2' => 'B', '3' => 'C');
			$unit[$k]['fee_mode'] = $fee_mode[$v['fee_mode']];
		}
		$this->data['unit'] = $unit;
		$this->load->view('domain/bind_search', array('data' => $this->data));
	}

	function bind_form($id=0) {
		if ($id == 0) {
			echo 'Error';
			exit;
		} else {
			$this->load->model('unit');
			$unit = $this->unit->find($id);
			if (!$unit) {
				echo 'Error';
				exit;
			} else {
				$this->data['unit'] = $unit[0];
			}
		}
		$this->load->model('cdn_domain');
		$this->data['unbind_domain'] = $this->cdn_domain->get_unbind_domain();
		$this->load->view('domain/bind_form', array('data' => $this->data));
	}

	function bind_do() {
		$data['unit_id'] = $this->input->post('unit_id', TRUE);
		$data['domain_id'] = $this->input->post('domain', TRUE);

		$this->load->model('unit_bind');
		$r = $this->unit_bind->filter($data);
		if ($r['status']){
			$this->unit_bind->add($data);
			echo json_encode(array('r'=>'succ', 'msg' => 'ok'));
		} else {
			echo json_encode(array('r'=>'fail', 'msg' => $r['msg']));
		}
	}

	function bind_form2($id=0) {
		if ($id == 0) {
			echo 'Error';
			exit;
		} else {
			$this->load->model('unit');
			$unit = $this->unit->find($id);
			if (!$unit) {
				echo 'Error';
				exit;
			} else {
				$this->data['unit'] = $unit[0];
			}
		}
		$this->load->model('unit_bind');
		$this->load->model('cdn_domain');
		$r = $this->unit_bind->bdomain($id);
		$bind_domain = array();
		foreach ($r as $v) {
			$tmp = $this->cdn_domain->find($v['domain_id']);
			if ($tmp) $bind_domain[] = $tmp[0];
		}
		$this->data['bind_domain'] = $bind_domain;	
		$this->load->view('domain/bind_form2', array('data' => $this->data));
	}

	function bind_undo() {
		$data['unit_id'] = $this->input->post('unit_id', TRUE);
		$data['domain_id'] = $this->input->post('domain', TRUE);

		$this->load->model('unit_bind');
		if (empty($data['domain_id'])) {
			echo json_encode(array('r'=>'fail', 'msg' => '提交域名为空'));
		} else {
			foreach ($data['domain_id'] as $d) {
				$this->unit_bind->del(array('unit_id' => $data['unit_id'], 'domain_id' => $d));
			}
			echo json_encode(array('r'=>'succ', 'msg' => 'ok'));
		}
	}
	
}
